package com.orcamento.entity;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.OrderBy;

@Entity
@Table(name="CartaoDeCredito")
public class CreditCard {

	@Id
	@SequenceGenerator(name="creditcard_id", sequenceName="creditcard_id")
	@GeneratedValue(generator="creditcard_id", strategy=GenerationType.AUTO)
	@Column(name="IDCartaoCredito")
	private Long id;
	@Column(name="StNome", nullable=false)
	private String name;
	@Column(name="StMarca", nullable=false)
	private String brand;
	@Column(name="StCobertura", nullable=false)
	private String coverage;
	@Column(name="BoAtivo", nullable=false)
	private Boolean active;
	
	@ManyToOne(targetEntity=Account.class)
	@JoinColumn(name="IDConta", referencedColumnName="IDPatrimonio", nullable=false)
	private Account account;
	
	@OneToMany(mappedBy="creditCard")
	@OrderBy(clause="year DESC, month DESC")
	private List<Invoice> invoices;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public String getCoverage() {
		return coverage;
	}
	public void setCoverage(String coverage) {
		this.coverage = coverage;
	}
	public List<Invoice> getInvoices() {
		return invoices;
	}
	public void setInvoices(List<Invoice> invoices) {
		this.invoices = invoices;
	}
	public Boolean getActive() {
		return active;
	}
	public void setActive(Boolean active) {
		this.active = active;
	}
	
	public Boolean hasInvoice(Invoice invoice) {
		for( Invoice savedInvoice : invoices ) {
			if( savedInvoice.equals( invoice ) ) {
				return true;
			}
		}
		return false;
	}
	public Account getAccount() {
		return account;
	}
	public void setAccount(Account account) {
		this.account = account;
	}
	
}
